package com.skyworth.web.wechat.service.impl;

import com.skyworth.web.order.domain.OrderModel;
import com.skyworth.web.order.domain.OrderNotifyModel;
import com.skyworth.web.order.domain.ParentOrderModel;
import com.skyworth.web.order.repository.OrderNotifyRepository;
import com.skyworth.web.order.repository.OrderRepository;
import com.skyworth.web.admin.table.repository.TableRepository;
import com.skyworth.web.order.repository.ParentOrderRepository;
import com.skyworth.web.order.repository.SubOrderRepository;
import com.skyworth.web.order.service.NewOrderService;
import com.skyworth.web.wechat.entity.TableBindModel;
import com.skyworth.web.wechat.repository.TableBindRepository;
import com.skyworth.web.wechat.service.TableBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by yuhaichao on 2016/9/7.
 */
@Service
public class TableBindServiceImpl implements TableBindService {

    @Autowired
    private TableBindRepository tableBindRepository;

    @Autowired
    private SubOrderRepository subOrderRepository;

    @Autowired
    private ParentOrderRepository parentOrderRepository;

    @Autowired
    private TableRepository tableRepository;

    @Autowired
    private OrderNotifyRepository orderNotifyRepository;

    @Override
    public TableBindModel getTableBindByUserId(String userId) {
        return tableBindRepository.getTableBindByUserId(userId);
    }

    @Override
    @Transactional
    public TableBindModel save(TableBindModel tbm){
        tableBindRepository.unbindTableByUserId(tbm.getUserId());
        return tableBindRepository.save(tbm);
    }

    @Override
    @Transactional
    public TableBindModel saveTableBind(TableBindModel tbm,OrderModel om) {
        if(om!=null) {
            ParentOrderModel pom = parentOrderRepository.getLastByTableId(tbm.getUserId());
            OrderModel subOrderModel = subOrderRepository.getOrderCart(tbm.getUserId());
            long parentId = 0;
            if (pom != null && pom.getId() > 0) {
                parentId = pom.getId();
            } else {
                pom = new ParentOrderModel();
                pom.setStoreId(om.storeId);
                pom.setTableId(tbm.getTableId());
                pom.setPeople(om.people);
                pom.setState(NewOrderService.ORDER_UNCONFIRMED);
                pom = parentOrderRepository.save(pom);
                parentId = pom.getId();
            }
            subOrderRepository.updateOrderCart(tbm.getTableId(), parentId, tbm.getUserId());
            OrderNotifyModel orderNotifyModel = new OrderNotifyModel();
            orderNotifyModel.setOrderId(subOrderModel.id);
            orderNotifyModel.setType(1);
            orderNotifyRepository.save(orderNotifyModel);
            if (tbm.getState() == NewOrderService.ORDER_CONFIRMED) {
                tableRepository.updateTableOrderState(tbm.getTableId());
            }
        }
        tableBindRepository.unbindTableByUserId(tbm.getUserId());
        return tableBindRepository.save(tbm);
    }

    @Override
    public void unbindTable(long tableId) {
        tableBindRepository.unbindTable(tableId);
    }
}
